import { MemoryVectorStore } from 'langchain/vectorstores/memory';
import { OpenAIEmbeddings } from '@langchain/openai';
import { config } from '../config/config';
import { Document } from '@langchain/core/documents';

export class VectorStoreService {
  private vectorStore: MemoryVectorStore;
  private embeddings: OpenAIEmbeddings;

  constructor() {
    this.embeddings = new OpenAIEmbeddings({
      openAIApiKey: config.dashscope.apiKey,
      configuration: {
        baseURL: config.dashscope.baseURL
      },
      modelName: 'text-embedding-v2'
    });
    this.vectorStore = new MemoryVectorStore(this.embeddings);
  }

  async addDocuments(documents: Document[]) {
    await this.vectorStore.addDocuments(documents);
  }

  async similaritySearch(query: string, k: number = 4) {
    return await this.vectorStore.similaritySearch(query, k);
  }
}
